package com.woniuxy.carlife.controller;


import com.alibaba.fastjson.JSON;
import com.woniuxy.carlife.Param.StoreBusinessParam;
import com.woniuxy.carlife.Param.StoreImageParam;
import com.woniuxy.carlife.Param.StoreParam;
import com.woniuxy.carlife.carlifecommonsweb.responsetemplate.Result;
import com.woniuxy.carlife.carlifecommonsweb.responsetemplate.StatusCode;
import com.woniuxy.carlife.domain.Store;
import com.woniuxy.carlife.domain.StoreInfo;
import com.woniuxy.carlife.domain.Tags;
import com.woniuxy.carlife.dto.StoreinfoDto;
import com.woniuxy.carlife.mapper.StoreMapper;
import com.woniuxy.carlife.mapper.TagsMapper;
import com.woniuxy.carlife.service.StoreService;
import com.woniuxy.carlife.util.MinioTemplate;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.sql.Timestamp;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @ClassName: StoreController
 *@Description: 门店前端控制器
 * @author: yanzhongliang     
 * @date: 2021-04-05
 */

@RestController
//@RequestMapping("/store")
@Slf4j
@Api(tags = "门店接口信息")//用于描述接口类的相关信息，作用于类上
public class StoreController {
    @Resource
    private RedisTemplate redisTemplate;
    @Resource
    private StoreService storeService;
    @Resource
    private StoreMapper storeMapper;
    //    引入文件上传配置
    @Resource
    private MinioTemplate mt;

    /**
     * 功能描述〈通过门店id查询门店详情,返回门店详情集合到前端〉
     * @author: yanzhongliang
     * @param: storeId
     * @return: List<StoreinfoDto>
     * @Date 2021/4/6 16:51
     * @throws 
     */

    //@ApiOperation用于描述接口方法，作用于方法上
    @ApiOperation(value = "通过门店id查询门店详情",notes = "<span style='color:red;'>用来查询门店详情的接口</span>")
    //@ApiResponses用于描述响应状态信息
    @ApiResponses({
            @ApiResponse(code = 40012,message = "查询成功",response = StoreinfoDto.class),
            @ApiResponse(code=40011,message = "查询为空"),
            @ApiResponse(code=70021,message = "ID为空")
    })
    //@ApiImplicitParams用于描述接口参数
    @ApiImplicitParams({
            //dataType:参数类型
            //paramType:参数由哪里获取     path->从路径中获取，query->?传参，body->ajax请求
            @ApiImplicitParam(name = "storeId",value = "门店ID",dataType = "int",paramType = "query",example = "1")
    })

    @GetMapping("/ordinary/StoreFindAll")
    public Result storeinfoDtoList(@ApiParam Integer storeId){
        log.info("storeinfoDtoList开始：参数：{}",storeId);
        //判断门店id是否为空
        if(storeId!=null){
            //    通过门店id查询门店详情,返回门店详情集合到前端
            List<StoreinfoDto> listStoreInfo= storeService.getfindListStoreInfo(storeId);
            //判断查询到的门店详情是否为空
            if (!listStoreInfo.isEmpty()){
                log.info("getfindListStoreInfo结束：参数：{}",listStoreInfo);
                //将查询到的门店详情转成json字符串
                String strJson= JSON.toJSONString(listStoreInfo);
                //存入redis
                redisTemplate.opsForHash().put("store","Info",strJson);
                log.info("redisTemplate.opsForHash().put结束：参数：{}",listStoreInfo);
                return new Result(true, StatusCode.SEARCH_SUCCESSFUL,"查询店铺详情成功",listStoreInfo);
            }else {
                return new Result(false, StatusCode.QUERY_IS_EMPTY,"输入门店ID有误，查询为空");
            }


        }else {
            return new Result(false, StatusCode.ID_EMPTY,"输入门店ID为空，查询失败");
        }

    }

    /**
     * 功能描述〈根据门店名查询门店供后台查看〉
     * @author: yanzhongliang
     * @param: storeId
     * @return: List<Store>
     * @Date 2021/4/9 10:45
     * @throws
     */

    //@ApiOperation用于描述接口方法，作用于方法上
    @ApiOperation(value = "通过门店名查询门店",notes = "<span style='color:red;'>用来根据门店名查询门店供后台查看的接口</span>")
    //@ApiResponses用于描述响应状态信息
    @ApiResponses({
            @ApiResponse(code = 40012,message = "查询成功",response = Store.class),
            @ApiResponse(code=70022,message = "门店名不存在"),
            @ApiResponse(code=70030,message = "门店名为空")
    })
    //@ApiImplicitParams用于描述接口参数
    @ApiImplicitParams({
            //dataType:参数类型
            //paramType:参数由哪里获取     path->从路径中获取，query->?传参，body->ajax请求
            @ApiImplicitParam(name = "storeName",value = "门店名",dataType = "String",paramType = "path",example = "李宁")
    })

    @GetMapping("/storefandList")
    public Result getStoreFindList( @ApiParam String storeName){
        log.info("getStoreFindList开始查询门店基础信息：参数：{}",storeName);
        //判断门店名是否为空
        if(storeName!=null){
            //调用查询门店基础信息
            List<Store> list=storeService.getStoreFindLists(storeName);
            //判断查询到的门店信息是否为空
            if (!list.isEmpty()){
                log.info("getStoreFindLists查询到门店基础信息：参数：{}",list);

        //将查询到的门店详情转成json字符串
        String strJson= JSON.toJSONString(list);
        // 存入redis
        redisTemplate.opsForHash().put("store","store",strJson);
                log.info("redisTemplate.opsForHash().put将查询到的门店基础信息存入redis：参数：{}",list);
                return new Result(true,StatusCode.SEARCH_SUCCESSFUL,"查询门店基础信息成功",list);
            }else {
                return new Result(false,StatusCode.STORENAME_EMPTY,"输入门店名有误，查询为空");
            }
        }else {
            return new Result(false,StatusCode.STORE_NAME_EMPTY,"输入门店名为空");
        }

    }

        /**
     * 功能描述〈修改门店图片〉
     * @author: yanzhongliang
     * @param: StoreImageParam
     * @return: storeImageParam.getstoreImage
     * @Date 2021/4/12 21:51
     * @throws
     */

    //@ApiOperation用于描述接口方法，作用于方法上
    @ApiOperation(value = "门店图片上传修改",notes = "<span style='color:red;'>用来上传门店图片修改的接口</span>")
    //@ApiResponses用于描述响应状态信息
    @ApiResponses({
            @ApiResponse(code = 40017,message = "修改成功",response =StoreImageParam.class),
            @ApiResponse(code=70020,message = "门店图片为空"),
            @ApiResponse(code=70021,message = "ID为空")
    })
    @PostMapping("/storeImage")
    public Result updateStoreImage(@ModelAttribute StoreImageParam stroeImageParam) {

        log.info("传入门店ID和门店图片修改开始：参数：{}",stroeImageParam);
        //判断传入门店id是否为空
        if (stroeImageParam.getStoreId()!=null){
            //判断门店图片是否为空
            if (stroeImageParam.getStoreImage()!=null){
                //调用数据层方法进行门店图片修改
                storeService.updateImage(stroeImageParam.getStoreId(),stroeImageParam.getStoreImage());
                return new Result(true, StatusCode.SUCCESSFULLY_MODIFIED, "修改门店图片成功");
            }else {
                return new Result(false, StatusCode.STORE_IMAGE_EMPTY, "门店图片为空，请选择要修改的门店图片");
            }
        }else {
            return new Result(false, StatusCode.ID_EMPTY, "输入门店ID为空");
        }
    }


    /**
     * 功能描述〈修改门店营业执照〉
     * @author: yanzhongliang
     * @param:
     * @return:
     * @Date 2021/4/12 21:51
     * @throws
     */

    //@ApiOperation用于描述接口方法，作用于方法上
    @ApiOperation(value = "门店营业执照修改",notes = "<span style='color:red;'>用来根据门店ID修改门店营业执照的接口</span>")
    //@ApiResponses用于描述响应状态信息
    @ApiResponses({
            @ApiResponse(code = 40017,message = "修改成功",response = StoreBusinessParam.class),
            @ApiResponse(code=70024,message = "营业执照图片为空"),
            @ApiResponse(code=70021,message = "ID为空")
    })

    @PostMapping("/storeBusinessImage")
    public Result updateStoreBusiness(@ModelAttribute StoreBusinessParam storeBusinessParam) {

        log.info("传入门店ID和营业执照对象：参数：{}",storeBusinessParam);
        //判断门店id是否为空
        if (storeBusinessParam.getStoreId()!=null){
            //传入门店id不为空，再判断营业执照是否为空
            if (storeBusinessParam.getStoreBusiness()!=null){
                //调用数据层方法进行门店营业执照修改
                storeService.updateBusiness(storeBusinessParam.getStoreId(),storeBusinessParam.getStoreBusiness());
                return new Result(true, StatusCode.SUCCESSFULLY_MODIFIED, "修改营业执照成功");
            }else {
                return new Result(false, StatusCode.BUSSINESS_EMPTY, "营业执照为空，请选择要修改的门店营业执照");
            }
        }else {
            return new Result(false, StatusCode.ID_EMPTY, "传入门店ID为空");
        }
    }
}

